package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/goods")
public class DtsGoodsController {

    @Autowired
    DtsBrandService brandService;
    @Autowired
    GoodsService goodsService;

    @Autowired
    CateGoryService cateGoryService;

    @Autowired
    AttributeService attributeService;
    @Autowired
    SpecificationService specificationService;
    @Autowired
    ProductService productService;

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods){
        goodsService.delete(goods);
        return ResponseUtil.ok();
    }

    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone) {
        Object o = checkGoodsData(goodsAllinone);
        if (o != null) {
            return o;
        }
        goodsService.save(goodsAllinone);
        return ResponseUtil.ok();
    }

    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone) {
        Object result = checkGoodsData(goodsAllinone);
        if (result != null) {
            return result;
        }
        goodsService.update(goodsAllinone);
        return ResponseUtil.ok();
    }

    private Object checkGoodsData(GoodsAllinone goodsAllinone) {
        //获取商品对象
        DtsGoods goods = goodsAllinone.getGoods();
        if (goods == null) {
            return ResponseUtil.fail(-1, "商品不能为空");
        }

        if (StringUtils.isEmpty(goods.getName())) {
            return ResponseUtil.fail(-1, "商品名称不能为空");
        }

        if (StringUtils.isEmpty(goods.getGoodsSn())) {
            return ResponseUtil.fail(-1, "商品编号不能为空");
        }

        //获取品牌商的id
        Integer brandId = goods.getBrandId();
        //如果品牌商id不为空，则需要判断id是否是有效的
        if (brandId != null) {
            DtsBrand brand = brandService.selectById(brandId);
            if (brand == null) {
                return ResponseUtil.fail(-1, "品牌商id无效");
            }
        }
        //获取分类id
        Integer categoryId = goods.getCategoryId();
        if (categoryId != null) {
            DtsCategory category = cateGoryService.selectById(categoryId);
            if (category == null) {
                return ResponseUtil.fail(-1, "分类id无效");
            }
        }

        //获取商品的属性
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if (attributes != null && attributes.length > 0) {
            for (DtsGoodsAttribute attribute : attributes) {
                if (StringUtils.isEmpty(attribute.getAttribute())) {
                    return ResponseUtil.fail(-1, "商品属性名不能为空");
                }
            }
        }
        //获取商品的规格
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if (specifications != null && specifications.length > 0) {
            for (DtsGoodsSpecification specification : specifications) {
                if (StringUtils.isEmpty(specification.getSpecification())) {
                    return ResponseUtil.fail(-1, "商品规格名称不能为空");
                }
            }
        }
        //获取商品的库存
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        if (products != null && products.length > 0) {
            for (DtsGoodsProduct product : products) {
                if (StringUtils.isEmpty(product.getSpecifications().toString())) {
                    return ResponseUtil.fail(-1, "商品库存的规格不能为空");
                }
            }
        }

        return null;
    }

    @GetMapping("/detail")
    public Object detail(Integer id) {
        //商品的详情信息
        DtsGoods goods = goodsService.selectById(id);
        //查询商品的分类id
        Integer categoryL2Ids = goods.getCategoryId();
        //根据二级分类查询一级分类
        Integer categoryL1IdS = cateGoryService.findL1ById(categoryL2Ids);
        Integer[] categoryIds = {categoryL2Ids, categoryL1IdS};
        //查询商品的属性
        List<DtsGoodsAttribute> attributeList = attributeService.findAttributesByGoodsId(id);
        //查询商品的规格
        List<DtsGoodsSpecification> specificationList = specificationService.findAttributesByGoodsId(id);
        //查询商品的库存量
        List<DtsGoodsProduct> productList = productService.findAttributesByGoodsId(id);

        Map<String, Object> data = new HashMap<>();
        data.put("categoryIds", categoryIds);
        data.put("goods", goods);
        data.put("attributes", attributeList);
        data.put("specifications", specificationList);
        data.put("products", productList);
        return ResponseUtil.ok(data);

    }

    @GetMapping("/catAndBrand")
    public Object catAndBrand() {
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();

        List<Integer> brandIds = null;
        if (subject.hasRole("品牌制造商")) {
            brandIds = brandService.findBreadAdminId(admin.getId());
        }
        List<Map<String, Object>> brandList = brandService.findBrandList(brandIds);
        List<CatVo> categoryList = cateGoryService.findCategoryList();
        Map<String, Object> data = new HashMap<>();
        data.put("brandList", brandList);
        data.put("categoryList", categoryList);
        return ResponseUtil.ok(data);


    }

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
                       String goodsSn, String name, String sort, String order) {

        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();

        List<Integer> brandsId = null;
        if (subject.hasRole("品牌制造商")) {
            brandsId = brandService.findBreadAdminId(admin.getId());
        }
        Map<String, Object> data = goodsService.queryPage(page, limit, goodsSn, name, sort, order, brandsId);

        return ResponseUtil.ok(data);
    }
}
